﻿using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

namespace UIExpand
{
    public class GrayImage : Image
    {
        private static Material m_SharedMat;
        private Material changedMat;
        public override Material material
        {
            get
            {
                initShareMat();

                if (changedMat != null)
                {
                    return changedMat;
                }
                else if (m_SharedMat != null)
                {
                    return m_SharedMat;
                }

                return defaultMaterial;
            }
            set
            {
                initShareMat();
                changedMat = value;
                if (value != null && value.shader != null && m_SharedMat.shader.name == value.shader.name)
                {
                    changedMat = null;
                }
                SetMaterialDirty();
            }
        }

        private void initShareMat()
        {
            if (m_SharedMat == null)
            {
                Shader s = Shader.Find("UI/GrayImage");
                m_SharedMat = new Material(s);
            }
        }

        public void SetGray(bool gray)
        {
            if(gray)
                color = Color.black;
            else
                color = Color.white;
        }

        public void SetBrightness(float brightness)
        {
            material.SetFloat("_Brightness", brightness);
        }
    }
}